package com.hn.vue.service.impl;

import com.hn.vue.mapper.MenuMapper;
import com.hn.vue.pojo.Menu;
import com.hn.vue.pojo.MenuExample;
import com.hn.vue.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * hanni
 * 2019/3/4 0004
 * 10:47
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;
    @Override
    public List<Menu> getMenu() {
        return toTree(0L,"顶级菜单");
    }

    private List<Menu> toTree(Long parentId,String parentName){

        MenuExample example = new MenuExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        example.setOrderByClause("order_num asc");
        List<Menu> menus = menuMapper.selectByExample(example);
        for (Menu menu : menus) {
            menu.setParentName(parentName);
            menu.setCreateTime(null);
            menu.setLastUpdateTime(null);
            List<Menu> children = toTree(menu.getId(), menu.getName());
            menu.setChildren(children);
        }
        return menus;
    }


    @Override
    public void save(Menu menu) {
        if(menu.getId() == null){
            menu.setDelFlag((byte)0);
            menuMapper.insert(menu);
        }else{
            menuMapper.updateByPrimaryKeySelective(menu);
        }

    }

    @Override
    public List<Menu> findMenusByUsername(String username) {



        return menuMapper.findMenusByUsername(username);
    }


}
